﻿package tk.mystudio.permission.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import tk.mystudio.permission.constant.AccountState;


/**
 * 账户类(登录用)	 
 * @author jiangkunpeng
 * @since 2011-3-16 
 * @version 1.0
 */
@Entity
@Table(name = "account")
public class Account implements UserDetails, Serializable {

	private static final long serialVersionUID = 6491740041172310274L;

	/**
	 * 账户编号
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", columnDefinition = "int")
	protected Integer id;
	
	/**
	 * 用户名唯一
	 */
	@Column(name = "name", unique = true, columnDefinition = "varchar(45)")
	protected String name;
	
	/**
	 * 加密后的密码(salt为用户名)
	 */
	@Column(name = "password", columnDefinition = "varchar(32)")
	protected String password;
	
	/**
	 * 创建时间
	 */
	@Column(name = "create_time", columnDefinition = "timestamp")
	@Temporal(TemporalType.TIMESTAMP)
	protected Date createTime;
	
	/**
	 * 更新时间
	 */
	@Column(name = "update_time", columnDefinition = "timestamp")
	@Temporal(TemporalType.TIMESTAMP)
	protected Date updateTime;
	
	/**
	 * 上次登录时间
	 */
	@Column(name = "last_login_time", columnDefinition = "timestamp")
	@Temporal(TemporalType.TIMESTAMP)
	protected Date lastLoginTime;
	
	/**
	 * 上次登录IP
	 */
	@Column(name = "last_login_ip", columnDefinition = "varchar(20)")
	protected String lastLoginIp;
	
	/**
	 * 账户状态 详见{@link tk.mystudio.permission.constant.AccountState 账户状态常量类}
	 */
	@Column(name = "state", columnDefinition = "tinyint")
	protected Integer state;
	
	@ManyToMany(fetch = FetchType.LAZY)  
	@JoinTable(name = "account_role", inverseJoinColumns = @JoinColumn(name = "role_id"), joinColumns = @JoinColumn(name = "account_id"))  
	protected List<Role> roles;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	@Override
	public int hashCode() {
		return super.hashCode()+id;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Account) {
			Account account = (Account) obj;
			if(account.getId()==id && name.equals(account.getName())) {
				return true;
			}
		}
		return false;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Account [createTime=");
		builder.append(createTime);
		builder.append(", id=");
		builder.append(id);
		builder.append(", lastLoginIp=");
		builder.append(lastLoginIp);
		builder.append(", lastLoginTime=");
		builder.append(lastLoginTime);
		builder.append(", name=");
		builder.append(name);
		builder.append(", password=");
		builder.append(password);
		builder.append(", roles=");
		builder.append(roles);
		builder.append(", state=");
		builder.append(state);
		builder.append(", updateTime=");
		builder.append(updateTime);
		builder.append("]");
		return builder.toString();
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		if (null!=roles) {
			for (Role role : roles) {
				authorities.add(new GrantedAuthorityImpl(role.getName()));
			}
		}
		return authorities;
	}

	@Override
	public String getUsername() {
		return name;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;	//账户不过期
	}

	@Override
	public boolean isAccountNonLocked() {
		return state!=AccountState.LOCKED;	//账户不锁定
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;	//凭证(密码)不到期
	}

	@Override
	public boolean isEnabled() {
		return state==AccountState.ACTIVE;		//账户是否启用
	}
	
}
